package com.e3.basdoc.dept.service.impl;

import java.util.List;

import com.e3.basdoc.dept.dao.DeptDAO;
import com.e3.basdoc.dept.entity.DeptEntity;
import com.e3.basdoc.dept.model.DeptModel;
import com.e3.basdoc.dept.service.DeptService;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SessionContext;
import com.e3.core.base.SuperService;
import com.e3.web.core.model.SuperModel;

public class DeptServiceImpl extends SuperService implements DeptService {

	private DeptDAO deptDAO;

	private DeptDAO getDeptDAO() {
		if (deptDAO == null) {
			deptDAO = DAOHelper.createDAOInstance(DeptDAO.class);
		}
		return deptDAO;
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptService#newDept(com.e3.basdoc.dept.model.DeptModel)
	 */
	public DeptModel newDept(DeptModel deptModel) throws ServiceException {
		if (deptModel.getCorpId() == null || "".equals(deptModel.getCorpId())) {
			deptModel.setCorpId(SessionContext.getCorpID() + "");
		}
		validateDept(deptModel);
		checkDeptExisted(deptModel);

		DeptEntity dept = deptModelToEntity(deptModel, null);
		deptModel.setId(dept.getId());

		getDeptDAO().persist(dept);

		return deptModel;
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptService#modifyDept(com.e3.basdoc.dept.model.DeptModel)
	 */
	public DeptModel modifyDept(DeptModel deptModel) throws ServiceException {
		if (deptModel.getCorpId() == null || "".equals(deptModel.getCorpId())) {
			deptModel.setCorpId(SessionContext.getCorpID() + "");
		}
		validateDept(deptModel);

		long id = deptModel.getId();

		DeptEntity entity = getDeptDAO().getById(DeptEntity.class, id);
		if (entity == null) {
			newDept(deptModel);
			return deptModel;
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(deptModel.getCode())) {
			checkDeptExisted(deptModel);
		}

		deptModelToEntity(deptModel, entity);

		getDeptDAO().merge(entity);

		return deptModel;
	}

	private void validateDept(DeptModel deptModel) throws ServiceException {
		String code = deptModel.getCode();
		String name = deptModel.getName();

		if (code == null || code.trim().length() == 0 || name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置部门的编码和名称。");
		}
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptService#checkDeptExisted(com.e3.basdoc.dept.model.DeptModel)
	 */
	public void checkDeptExisted(DeptModel deptModel) throws ServiceException {
		if (deptModel == null) {
			return;
		}

		String code = deptModel.getCode();
		String name = deptModel.getName();
		DeptEntity entity = getDeptDAO().getCorpDeptByCode(code, new Long(deptModel.getCorpId()));
		if (entity != null) {
			throw new ServiceException("编码为'" + code + "',名称为'" + name + "'已经存在，请设置新的编码和名称。");
		}
	}

	/* (non-Javadoc)
	 * @see com.e3.basdoc.dept.service.impl.DeptService#deleteDepts(java.util.List)
	 */
	public void deleteDepts(List<DeptModel> deptModels) throws ServiceException {
		if (deptModels == null || deptModels.isEmpty()) {
			return;
		}
		Long[] ids = getIds(deptModels);
		getDeptDAO().deleteDeptByIds(ids);
		getDeptDAO().deleteDeptChildren(ids);
	}

	private Long[] getIds(List<? extends SuperModel> models) {
		if (models == null || models.isEmpty()) {
			return null;
		}
		Long[] ids = new Long[models.size()];
		for (int i = 0; i < models.size(); i++) {
			Long id = models.get(i).getId();
			ids[i] = id;
		}
		return ids;
	}

	private DeptEntity deptModelToEntity(DeptModel deptModel, DeptEntity entity) {
		DeptEntity dept = super.modelToEntity(DeptEntity.class, deptModel, entity);
		
		dept.setCode(deptModel.getCode());
		dept.setName(deptModel.getName());
		dept.setContactAddress(deptModel.getContactAddr());
		dept.setContactPerson(deptModel.getContactPerson());
		dept.setContactWay(deptModel.getContactWay());
		dept.setEmail(deptModel.getEmail());
		dept.setFax(deptModel.getFax());
		dept.setFoundTime(deptModel.getFoundTime().getTime());
		dept.setStatus(new Integer(deptModel.getStatus()));
		dept.setParentId(deptModel.getParentId());
		dept.setRegionId(deptModel.getRegionId());
		dept.setManager(deptModel.getManager());
		dept.setOfficeAddress(deptModel.getOfficeAddress());

		dept.setRemark(deptModel.getRemark());
		dept.setShortName(deptModel.getShortName());
		dept.setTelphone(deptModel.getTelphone());
		dept.setType(new Integer(deptModel.getType()));
		dept.setZipcode(deptModel.getZipcode());

		return dept;
	}

}
